package com.itheima.admin.controller;

import com.itheima.admin.service.IModuleService;
import com.itheima.pojo.Config.Result;
import com.itheima.pojo.dto.PageDto;
import com.itheima.pojo.vo.ModuleVo;
import com.itheima.pojo.vo.ModuleVoAll;
import com.itheima.pojo.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/module")
@Api(value = "模块接口",tags = "admin_module",description = "用于实现后台模块的增删改查操作")
public class ModuleController {
    @Autowired
    private IModuleService moduleService;

    @PostMapping("/list")
    public PageVo<ModuleVo> QueryByPage(@RequestBody PageDto pageDto){
        if (pageDto.getCurrentPage() == null) {
            pageDto.setCurrentPage(1);
        }
        if (pageDto.getPageSize() == null) {
            pageDto.setPageSize(10);
        }
        return moduleService.queryByPage(pageDto);
    }

    @GetMapping("/listall")
    public List<ModuleVoAll> selectAll(){
        return moduleService.queryAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除模块")
    public Result deleteById(@PathVariable("id") String id){
        boolean flag=moduleService.deleteById(id);
        Result result = Result.builder()
                .flag(flag)
                .message(flag?"删除模块成功!":"删除模块失败")
                .build();
        return result;
    }
}
